import Taro from '@tarojs/taro';
import {
  GET_DISTRIBUTOR_INFO,
  GET_DISTRIBUTOR_TOP_INFO,
  GET_POSTER,
  GET_WORDS_POSTER,
  HOTEL_DETAIL_POSTER,
  LECTURE_ROOM_POSTER_INFO,
  LIAN_POSTER_INFO,
  MOVIE_POSTER,
  MY_POSTER_INFO,
  NEWS_GET_POSTER,
  POLYMERIZE_POSTER,
  POSTER_INFO,
  PRODUCT_DETAIL_POSTER,
  SCENIC_DETAIL_POSTER,
  URL_RESIZE,
  TOURIST_DETAIL_POSTER,
  INDEX_POSTER_INFO,
  ALI_POSTER_INFO,
  IDENTITY_IS_SELLER,
  FETCH_SECKILL_LIST_POSTER_INFO,
  FETCH_SECKILL_LIST_POSTER
} from "../constants/api"
import {http} from './http'
import {Poster} from "./server.interface"
import {strlen} from './tools'
import {platform} from "./platform-settting.service";
import {newHttp} from "./request";
import {user} from "./user.service";

let isSeller = false
const titleConvert = (title: string, length = 32) => {
  let str = ''
  if (title.length > length) {
    str = title.substr(0, length) + '...'

  } else {
    str = title
  }
  return str
}
const titleConvertSubstrOne = (title: string, length = 20) => {
  let str = ''
  str = title.substr(0, length)
  return str
}
const titleConvertSubstrTwo = (title: string, length = 20) => {
  let str = ''
  str = title.substr(20, length)
  if (title.length > 35) {
    str += '...'
  }
  return str
}

const titleConvertSubstrTwoKD = (title: string, length = 20, from) => {
  let str = ''
  str = title.substr(from, length)
  if (title.length > 18) {
    str += '...'
  }
  return str
}

export interface Tempalte1 {
  imgUrl: string
  price: number
  market_price: number
  uuid: string
  isDetail?: string
}

interface PosterItemImage {
  type: 'image' | 'text'
  url: string
  coord: string
  op?: string
}

interface PosterItemText {
  type: 'image' | 'text'
  coord: string
  "font-size"?: number
  text: string
  color?: string
}

export interface Tempalte2 {
  posterImgUrl: string
  title: string
  QRcode: string
  time: string
  name: string
  shareName: string
}

const getPosterTemplate0 = (data: Poster): string => {
  const tempalteArr: (PosterItemImage | PosterItemText)[] = []
  const item1: PosterItemImage = {
    type: 'image',
    url: data.goodsBackground || '',
    coord: '720x1030!+0+0'
  }
  const item2: PosterItemImage = {
    type: 'image',
    url: data.siteLogo || '',
    coord: "180x180!+525+1050"
  }
  const item3: PosterItemImage = {
    type: 'image',
    url: data.wxVCodeImg,
    coord: '180x180!+25+1050',
  }
  const item4: PosterItemText = {
    type: 'text',
    coord: '+45+1230',
    text: '长按识别二维码',
    "font-size": 22
  }
  const item5: PosterItemText = {
    type: 'text',
    coord: '+260+1090',
    "font-size": 32,
    text: data.sellerPostName
  }
  const item6: PosterItemText = {
    type: 'text',
    coord: '+260+1140',
    "font-size": 28,
    color: '#009944',
    text: data.sellerPhone,
  }
  const item7: PosterItemText = {
    type: 'text',
    coord: '+260+1210',
    "font-size": 23,
    color: '#333333',
    text: isSeller ? data.sellerSlogan : '',
  }
  tempalteArr.push(item1, item2, item3, item4, item5, item6, item7)
  return JSON.stringify(tempalteArr)
}
const getPosterTemplateLianZero = (posterInfo: Poster, good, localMealId) => {
  const tempalteArr: (PosterItemImage | PosterItemText)[] = []
  const item2: PosterItemImage = {
    type: 'image',
    url: posterInfo.goodsBackground || '',
    coord: '720x360!+0+350',
  }
  const item3: PosterItemImage = {
    type: 'image',
    url: posterInfo.siteLogo || '',
    coord: "180x180!+525+1050"
  }
  const item4: PosterItemImage = {
    type: 'image',
    url: posterInfo.wxVCodeImg,
    coord: '180x180!+25+1050',
  }
  const item5: PosterItemText = {
    type: "text",
    "font-size": 40,
    color: "199739",
    text: titleConvert(good.title || '', 16),
    coord: `+${(36 - strlen(titleConvert(good.title || '', 16))) * 10}+180`
  }
  const item6: PosterItemText = {
    type: 'text',
    coord: `+${Math.floor((75 - strlen(titleConvert(good.description || '', 20))) * 4.4)}+250`,
    color: "#666666",
    "font-size": 24,
    text: titleConvert(good.description || '', 20)
  }
  const item7: PosterItemText = {
    type: 'text',
    coord: `+${Math.floor((36 - strlen(titleConvert(good.specList[localMealId].salePrice || '', 20))) * 9)}+830`,
    color: "#CA0000",
    "font-size": 34,
    text: '¥' + titleConvert(good.specList[localMealId].salePrice || '', 32)
  }
  const item8: PosterItemText = {
    type: 'text',
    coord: `+${Math.floor((56 - strlen(titleConvert(good.valid_date || '', 20))) * 4.9)}+890`,
    color: "#666666",
    "font-size": 24,
    text: '使用日期   ' + titleConvert(good.valid_date || '', 32)
  }
  const item9: PosterItemText = {
    type: 'text',
    coord: `+45+1230`,
    "font-size": 22,
    text: '长按图片购买'
  }
  const item10: PosterItemText = {
    type: 'text',
    coord: `+260+1090`,
    "font-size": 32,
    text: posterInfo.sellerPostName
  }
  const item11: PosterItemText = {
    type: 'text',
    coord: `+260+1140`,
    color: '#009944',
    "font-size": 28,
    text: posterInfo.sellerPhone
  }
  tempalteArr.push(item2, item3, item4, item5, item6, item7, item8, item9, item10, item11)
  return JSON.stringify(tempalteArr)
}

const getPosterTemplateKD = async (posterInfo: any) => {
  const tempalteArr: (PosterItemImage | PosterItemText)[] = []
  let httpUrl = posterInfo.goodsBackground || ''
  const data = await newHttp.post(URL_RESIZE, { httpUrl, w: 605, h: 550 })
  httpUrl = data.data
  const item2: PosterItemImage = {
    type: 'image',
    url: httpUrl,
    coord: '605x550!+73+136',
  }
  const item3: PosterItemImage = {
    type: 'image',
    url: posterInfo.siteLogo || '',
    op: `resize,w_80,h_80/circle,r_80`,
    coord: `80x80!+155+1027`
  }
  const item4: PosterItemImage = {
    type: 'image',
    url: posterInfo.wxVCodeImg,
    coord: `208x208!+498+981`
  }
  const item5: PosterItemText = {
    type: "text",
    "font-size": 26,
    color: "#666666",
    text: titleConvert(posterInfo.name || '', 22),
    coord: `+70+715`
  }
  if(!posterInfo.subTitle) {
    posterInfo.subTitle = ''
  }
  const item6 = {
    type: 'text',
    coord: `+70+755`,
    color: "#000000",
    "font-size": 34,
    text: posterInfo.subTitle.length > 19 ? titleConvertSubstrOne(posterInfo.subTitle || '', 19) : titleConvert(posterInfo.subTitle || '', 19)
  }
  let item15 = {}
  if (posterInfo.subTitle.length > 19) {
    item15 = {
      type: 'text',
      coord: `+70+800`,
      color: "#000000",
      "font-size": 34,
      text: titleConvertSubstrTwoKD(posterInfo.subTitle || '', 18, 19)
    }
  }
  const item9: PosterItemText = {
    type: 'text',
    coord: `+240+1060`,
    "font-size": 20,
    "color": '#333333',
    text: posterInfo.sellerPostName
  }
  const item12: PosterItemText = {
    type: 'text',
    coord: `+68+895`,
    color: '#FE0000',
    "font-size": 28,
    text: '¥'
  }
  const item13: PosterItemText = {
    type: 'text',
    coord: `+90+872`,
    color: '#FE0000',
    "font-size": 60,
    text: `${posterInfo.price}`
  }
  const strLength = String(posterInfo.price).length
  const item14: PosterItemText = {
    type: 'text',
    coord: `+${165 + (strLength > 1 ? strLength * ((strLength === 3) ? 31 : 30) : 20)}+887`,
    color: '#888888',
    "font-size": 28,
    text: `原价:${posterInfo.marketPrice}元`
  }
  const item16: PosterItemText = {
    type: 'text',
    coord: `+${105 + (strLength > 1 ? strLength * ((strLength === 3) ? 31 : 30) : 20)}+887`,
    color: '#888888',
    "font-size": 28,
    text: `起`
  }
  tempalteArr.push(item2, item3, item4, item5, item6, item9, item12, item13, item14, item15,item16)
  return JSON.stringify(tempalteArr)
}

const getPosterTemplate1_1 = (posterInfo: any, posterType = '0', good?: any, type? = '') => {
  const tempalteArr: (PosterItemImage | PosterItemText)[] = []
  const itemH = {
    item3H: 680, item4H: 655, item5H: 550, item6H: 600, item7H: 680, item8H: 750,
    item9H: 700, item10H: 740, item11H: 720, item12H: 655, item13H: 645, item14H: 650,
    item15H: 615, item16H: 660, item17H: 730, item18H: 640
  }
  if (posterInfo.shortName) {
    Object.keys(itemH).forEach(function (key) {
      itemH[key] += 50
    })
  }
  const item2: PosterItemImage = {
    type: 'image',
    url: posterInfo.goodsBackground || '',
    coord: '530x530!+0+0',
  }
  const item3: PosterItemImage = {
    type: 'image',
    url: posterInfo.siteLogo || '',
    coord: `90x90!+25+` + itemH.item3H
  }
  if (posterType == '1') {
    item3.coord = `90x90!+20+` + (posterInfo.shortName ? 734 : 680)
    item3.op = `resize,w_70,h_70/circle,r_70`
  }
  const item4: PosterItemImage = {
    type: 'image',
    url: posterInfo.wxVCodeImg,
    coord: '85x85!+425+' + itemH.item4H,
  }
  if (posterType == '1') {
    item4.coord = `100x100!+420+` + (itemH.item18H - (platform.info.codeWay === 0 ? 15 : 9))
  }
  const item5: PosterItemText = {
    type: "text",
    "font-size": 28,
    color: "000000",
    text: titleConvert(posterInfo.shortName || '', 20),
    coord: `+30+550`
  }
  if (type === 'ali') {
    posterInfo.name = good.title
    posterInfo.price = good.activityPrice
    posterInfo.marketPrice = good.originalPrice
  }
  let item6 = {
    type: 'text',
    coord: `+30+600`,
    color: "#000000",
    "font-size": 22,
    text: posterInfo.name.length > 20 ? titleConvertSubstrOne(posterInfo.name || '', posterType == '1' ? 30 : 20) : titleConvert(posterInfo.name || '', 20)
  }
  if (posterType == '1') {
    item6.color = '#888888'
  }
  let item15: any = {}
  if (posterInfo.name.length > 20) {
    item15 = {
      type: 'text',
      coord: `+30+630`,
      color: "#000000",
      "font-size": 22,
      text: titleConvertSubstrTwo(posterInfo.name || '', posterType == '1' ? 30 : 20)
    }
  }
  if (posterType == '1') {
    item15.color = '#888888'
  }
  if (!posterInfo.shortName) {
    item6 = {
      type: 'text',
      coord: `+30+550`,
      color: "#000000",
      "font-size": 22,
      text: posterInfo.name.length > 20 ? titleConvertSubstrOne(posterInfo.name || '', 20) : titleConvert(posterInfo.name || '', 20)
    }
    if (posterInfo.name.length > 20) {
      item15 = {
        type: 'text',
        coord: `+30+580`,
        color: "#000000",
        "font-size": 22,
        text: titleConvertSubstrTwo(posterInfo.name || '', 20)
      }
    }
  }
  // ${Math.floor((56 - strlen(titleConvert(posterInfo.name || '', 20))) * 6.4)}
  const item8: PosterItemText = {
    type: 'text',
    coord: `+435+` + (posterType == '1' ? itemH.item17H : itemH.item8H),
    "font-size": 12,
    text: '长按识别购买'
  }
  const item9: PosterItemText = {
    type: 'text',
    coord: `+${posterType == '1' ? 105 : 120}+` + (posterType == '1' ? itemH.item3H + 10 : itemH.item9H),
    "font-size": 16,
    text: posterInfo.sellerPostName
  }
  if (posterType == '1') {
    item9.color = '#333333'
  }
  const item10: PosterItemText = {
    type: 'text',
    coord: `+${posterType == '1' ? 105 : 120}+${(posterType == '1' ? itemH.item17H : itemH.item10H)}`,
    color: '#009944',
    "font-size": 14,
    text: posterInfo.sellerPhone
  }
  const item11: PosterItemText = {
    type: 'text',
    coord: `+${posterType == '1' ? 105 : 120}+${(posterType == '1' ? (itemH.item11H - 9) : itemH.item10H)}`,
    color: '#333333',
    "font-size": 12,
    text: isSeller ? posterInfo.sellerSlogan : ''
  }
  if (posterType == '1') {
    item11.color = '#888888'
  }
  const item12: PosterItemText = {
    type: 'text',
    coord: `+30+${(posterType == '1' ? itemH.item15H + 22 : itemH.item12H)}`,
    color: '#FE0000',
    "font-size": posterType == '1' ? 16 : 14,
    text: '¥'
  }
  const item13: PosterItemText = {
    type: 'text',
    coord: `+45+${(posterType == '1' ? itemH.item15H + 10 : itemH.item13H)}`,
    color: '#FE0000',
    "font-size": posterType == '1' ? 34 : 30,
    text: `${posterInfo.price}`
  }
  const item14: PosterItemText = {
    type: 'text',
    coord: `+${Math.floor(105 + String(posterInfo.price).length * (String(posterInfo.price).length >= 4 ? 8 : 4))}+${(posterType == '1' ? itemH.item15H + 20 : itemH.item14H)}`,
    color: '#999999',
    "font-size": 15,
    text: `市场价 ¥${posterInfo.marketPrice}`
  }
  tempalteArr.push(item2, item3, item4, item5, item6, item8, item9, item10, item11, item11, item12, item13, item14, item15)
  if (posterType == '1') {
    const strLength = String(posterInfo.price).length
    const item16: PosterItemText = {
      type: 'text',
      coord: `+${Math.floor(52 + (strLength > 1 ? strLength * 17.5 : 23))}+${itemH.item15H + 20}`,
      color: '#999999',
      "font-size": 15,
      text: `起`
    }
    tempalteArr.push(item16)
  }
  if (posterInfo.goods && posterInfo.goods.length > 0) {
    posterInfo.goods.map(_ => {
      return ''
    })
  }
  return JSON.stringify(tempalteArr)
}

const getDaLvPosterTemplate0 = (data: Poster): string => {
  const tempalteArr: (PosterItemImage | PosterItemText)[] = []
  const item1: PosterItemImage = {
    type: 'image',
    url: data.goodsBackground || '',
    coord: "750x1130!+0+0"
  }
  const item2: PosterItemImage = {
    type: 'image',
    url: data.siteLogo || '',
    coord: "180x180!+530+1130"
  }
  const item3: PosterItemImage = {
    type: 'image',
    url: data.wxVCodeImg,
    coord: '180x180!+20+1130',
  }
  const item4: PosterItemText = {
    type: 'text',
    coord: '+30+1300',
    text: '长按识别二维码',
    "font-size": 22
  }
  const item5: PosterItemText = {
    type: 'text',
    coord: '+250+1170',
    "font-size": 32,
    text: data.sellerPostName
  }
  const item6: PosterItemText = {
    type: 'text',
    coord: '+250+1220',
    "font-size": 28,
    color: '#009944',
    text: data.sellerPhone,
  }
  const item7: PosterItemText = {
    type: 'text',
    coord: '+250+1285',
    "font-size": 23,
    color: '#333333',
    text: isSeller ? data.sellerSlogan : '',
  }
  tempalteArr.push(item1, item2, item3, item4, item5, item6, item7)
  return JSON.stringify(tempalteArr)
}
const getDaLvPosterTemplate1_0 = (posterInfo: Poster) => {
  const tempalteArr: (PosterItemImage | PosterItemText)[] = []
  const item1: PosterItemImage = {
    type: 'image',
    url: posterInfo.siteLogo || '',
    coord: "120x120!+300+30",
    op: "resize,w_120,h_120/circle,r_120",
  }
  const item2: PosterItemImage = {
    type: 'image',
    url: posterInfo.goodsBackground || '',
    coord: '720x360!+0+350',
  }
  const item3: PosterItemImage = {
    type: 'image',
    url: posterInfo.siteLogo || '',
    coord: "180x180!+525+1050"
  }
  const item4: PosterItemImage = {
    type: 'image',
    url: posterInfo.wxVCodeImg,
    coord: '180x180!+25+1050',
  }
  const item5: PosterItemText = {
    type: "text",
    "font-size": 40,
    color: "199739",
    text: titleConvert(posterInfo.shortName || '', 20),
    coord: `+${(36 - strlen(titleConvert(posterInfo.shortName || '', 20))) * 10}+180`
  }
  const item6: PosterItemText = {
    type: 'text',
    coord: `+${Math.floor((56 - strlen(titleConvert(posterInfo.name || '', 20))) * 6.4)}+250`,
    color: "#666666",
    "font-size": 24,
    text: titleConvert(posterInfo.name || '', 20)
  }
  const item7: PosterItemText = {
    type: 'text',
    coord: `+${Math.floor((56 - strlen(titleConvert(posterInfo.effectiveDate || '', 20))) * 6.4)}+850`,
    color: "#666666",
    "font-size": 24,
    text: titleConvert(posterInfo.effectiveDate || '', 20)
  }
  const item8: PosterItemText = {
    type: 'text',
    coord: `+45+1230`,
    "font-size": 22,
    text: '长按图片购买'
  }
  const item9: PosterItemText = {
    type: 'text',
    coord: `+260+1090`,
    "font-size": 32,
    text: posterInfo.sellerPostName
  }
  const item10: PosterItemText = {
    type: 'text',
    coord: `+260+1140`,
    color: '#009944',
    "font-size": 28,
    text: posterInfo.sellerPhone
  }
  const item11: PosterItemText = {
    type: 'text',
    coord: `+260+1210`,
    color: '#333333',
    "font-size": 23,
    text: isSeller ? posterInfo.sellerSlogan : ''
  }

  tempalteArr.push(item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11)
  return JSON.stringify(tempalteArr)
}
const getDaLvPosterTemplate1_1 = (posterInfo: Poster) => {
  const tempalteArr: (PosterItemImage | PosterItemText)[] = []
  const item1: PosterItemImage = {
    type: 'image',
    url: posterInfo.siteLogo || '',
    coord: "120x120!+270+30",
    op: "resize,w_120,h_120/circle,r_120",
  }
  const item2: PosterItemImage = {
    type: 'image',
    url: posterInfo.goodsBackground || '',
    coord: '720x360!+0+350',
  }
  const item3: PosterItemImage = {
    type: 'image',
    url: posterInfo.siteLogo || '',
    coord: "180x180!+525+1050"
  }
  const item4: PosterItemImage = {
    type: 'image',
    url: posterInfo.wxVCodeImg,
    coord: '180x180!+25+1050',
  }
  const item5: PosterItemText = {
    type: "text",
    "font-size": 40,
    color: "199739",
    text: titleConvert(posterInfo.shortName || '', 20),
    coord: `+${(36 - strlen(titleConvert(posterInfo.shortName || '', 20))) * 10}+180`
  }
  const item6: PosterItemText = {
    type: 'text',
    coord: `+${Math.floor((56 - strlen(titleConvert(posterInfo.name || '', 20))) * 6.4)}+250`,
    color: "#666666",
    "font-size": 24,
    text: titleConvert(posterInfo.name || '', 20)
  }
  const item8: PosterItemText = {
    type: 'text',
    coord: `+45+1230`,
    "font-size": 22,
    text: '长按图片购买'
  }
  const item9: PosterItemText = {
    type: 'text',
    coord: `+260+1090`,
    "font-size": 32,
    text: posterInfo.sellerPostName
  }
  const item10: PosterItemText = {
    type: 'text',
    coord: `+260+1140`,
    color: '#009944',
    "font-size": 28,
    text: posterInfo.sellerPhone
  }
  const item11: PosterItemText = {
    type: 'text',
    coord: `+260+1210`,
    color: '#333333',
    "font-size": 23,
    text: isSeller ? posterInfo.sellerSlogan : ''
  }

  tempalteArr.push(item1, item2, item3, item4, item5, item6, item8, item9, item10, item11)
  if (posterInfo.goods && posterInfo.goods.length > 0) {
    const height = 725 + Math.floor((300 - posterInfo.goods.length * 50) / 2)
    posterInfo.goods.map((item, index) => {
      if (index < 6) {
        tempalteArr.push(
          {
            type: 'text',
            coord: `+80+${height + index * 50}`,
            color: "#666666",
            "font-size": 32,
            text: item.name || ''
          } as PosterItemText
        )
        tempalteArr.push(
          {
            type: 'text',
            coord: `+520+${height + index * 50}`,
            color: "#e7722f",
            "font-size": 32,
            text: item.price || 0
          } as PosterItemText
        )
      }
      return ''
    })
  }
  return JSON.stringify(tempalteArr)
}

const getSharePosterTemplate = (data: Tempalte1): string => {
  return (
    `[{
            "type":"image",
            "url":"${data.imgUrl || ''}",
            "coord":"196x156!+30+140",
            "resize_m":"fill",
            "isDetail":"${data.isDetail || ''}"
        }]`
  )
}
const apiUrl = {
  index: INDEX_POSTER_INFO,
  polymerize: POLYMERIZE_POSTER,
  scenic: SCENIC_DETAIL_POSTER,
  lecture_room: LECTURE_ROOM_POSTER_INFO,
  hotel: HOTEL_DETAIL_POSTER,
  product: PRODUCT_DETAIL_POSTER,
  tourist: TOURIST_DETAIL_POSTER,
  limit: POSTER_INFO,
  news: NEWS_GET_POSTER,
  distributor: GET_DISTRIBUTOR_INFO,
  top_distributor: GET_DISTRIBUTOR_TOP_INFO,
  vip: MY_POSTER_INFO,
  movie: MOVIE_POSTER,
  words: GET_WORDS_POSTER,
  lianlian: LIAN_POSTER_INFO,
  ali: ALI_POSTER_INFO,
  seckillList: FETCH_SECKILL_LIST_POSTER,
  seckillDetail: POSTER_INFO
}

const getPosterError = () => {
  Taro.showToast({title: '获取海报链接失败！', icon: 'none'})
}

const getPosterInfoError = () => {
  Taro.showToast({title: '获取海报信息失败！', icon: 'none'})
}

const getPosterUrlForSeckillList = async (seckill_id: string) => {
  const posterRet = await http.post<string>(FETCH_SECKILL_LIST_POSTER, { seckill_id })
  if (posterRet) {
    return posterRet
  } else {
    return false
  }
}

const getPosterUrl = async (params: string, name: string, shortName = '', bk = '', force = true, isDetail = '', posterType = '') => {
  const dateTime = new Date().getTime()
  const newName = name ? name : dateTime
  const posterRet = await http.post<string>(GET_POSTER, {
    params,
    name: newName,
    force,
    bk: name === 'movie' ? shortName : bk,
    isDetail,
    shortName,
    posterType
  })
  if (posterRet) {
    return posterRet
  } else {
    return false
  }
}

const getPoster = async (type: 'index' | 'polymerize' | 'scenic' | 'hotel' | 'product' | 'tourist' | 'limit' | 'news' | 'distributor' | 'top_distributor' | 'vip' | 'movie' | 'words' | 'lianlian' | 'ali' | 'lecture_room', goodId: string, types?: string, tempSellerId?: any) => {
  let posterInfoRet: Poster | false = false
  if (process.env.DISTINGUISH_IDENTITY) {
    const identityIsSeller = await http.get(IDENTITY_IS_SELLER, {})
    isSeller = identityIsSeller.isSeller
  }
  if (type === 'index') {
    // 首页
    posterInfoRet = await http.post<Poster>(apiUrl[type], {})
  } else if (type === 'news') {
    // 新闻
    posterInfoRet = await http.post<Poster>(apiUrl[type] + '/' + goodId, {tempSellerId})
  } else if (type === 'top_distributor' || type === 'distributor' || type === 'vip') {
    // 上级分销商、分销商、会员
    posterInfoRet = await http.post<Poster>(apiUrl[type], {type: types, tempSellerId})
  } else if (type === 'movie' || type === 'lianlian' || type === 'ali' || type === 'polymerize') {
    // 电影票、连连、聚合页面    polymerize ---> 聚合页面需要 uuid
    posterInfoRet = await http.post<Poster>(apiUrl[type], {uuid: goodId, path: type === 'polymerize' ? types : goodId, tempSellerId})
  } else if (type === 'words') {
    // 线路推广
    posterInfoRet = await http.post<Poster>(apiUrl[type], {id: goodId, tempSellerId})
  } else if (type === 'lecture_room') {
    // 达人讲堂
    posterInfoRet = await http.post<Poster>(apiUrl[type] + goodId, {tempSellerId})
  } else if (type === 'seckillDetail') {
    // 秒杀活动详情
    posterInfoRet = await http.post<Poster>(apiUrl[type], {gUd: goodId, tempSellerId, seckill_id: types})
  } else {
    const params: any = {gUd: goodId, tempSellerId}
    if (!await user.checkLogin()) {
      params.pkeys = 'pkeys'
    }
    // 其它
    posterInfoRet = await http.post<Poster>(apiUrl[type], params)
  }
  if (posterInfoRet) {
    const posterInfo = posterInfoRet
    let ret: false | string = false
    if (posterInfo.imgCustomize) {
      ret = await getPosterUrl(posterInfo.imgCustomize, goodId, posterInfo.shortName || '', posterInfo.imgCustomizeKey, true, '', posterInfo.posterType);
    } else {
      if (process.env.P_ENV == 'hiyogo') {
        // 大旅默认的修改
        if (posterInfo.posterType === '0') {
          if (type === 'vip') {
            const name = 'vip_' + goodId
            ret = await getPosterUrl(getPosterTemplate0(posterInfo), name, posterInfo.shortName || '', posterInfo.imgCustomizeKeytrue, true, '', posterInfo.posterType)
          } else {
            ret = await getPosterUrl(getDaLvPosterTemplate0(posterInfo), goodId)
          }
        }
        if (posterInfo.posterType === '1') {
          if (type === 'limit') {
            ret = await getPosterUrl(getDaLvPosterTemplate1_0(posterInfo), goodId)
          } else {
            ret = await getPosterUrl(getDaLvPosterTemplate1_1(posterInfo), goodId)
          }
        }
      } else {
        // 自定义海报
        if (posterInfo.posterType === '0') {
          ret = await getPosterUrl(getPosterTemplate0(posterInfo), goodId, posterInfo.shortName || '', posterInfo.imgCustomizeKeytrue, true, '', posterInfo.posterType)
        }
        if (posterInfo.posterType === '1') {
          if (process.env.P_ENV === 'kd') {
            ret = await getPosterUrl(await getPosterTemplateKD(posterInfo), goodId, posterInfo.shortName || '', 'upload/images/20220128/1643362627643_112.png', true, '', '0')
          } else {
            ret = await getPosterUrl(getPosterTemplate1_1(posterInfo, posterInfo.posterType), goodId, posterInfo.shortName || '', '', true, '', posterInfo.posterType)
          }
        }
      }
    }
    if (ret) {
      return {url: ret}
    } else {
      getPosterError()
      return false
    }
  } else {
    getPosterInfoError()
    return false
  }
}

const getLianPosterInfo = async (data?: any, img?: string, good?: any, localMealId?: number, type?: any) => {
  let posterInfoRet: Poster | false = false
  let usedType = 'lianlian'
  if (type && type === 'ali') {
    usedType = type
  } else {
    usedType = 'lianlian'
  }
  posterInfoRet = await http.post<Poster>(apiUrl[usedType], {
    path: data.path + '&scene=' + data.scene,
    pageType: data.pageType,
    posterImg: img,
    tempSellerId: data.tempSellerId
  })
  if (posterInfoRet) {
    const posterInfo = posterInfoRet
    if (img && usedType !== 'ali') {
      posterInfo.goodsBackground = img
    }
    let ret: false | string = false
    if (posterInfo.imgCustomize) {
      ret = await getPosterUrl(posterInfo.imgCustomize, usedType, posterInfo.imgCustomizeKey);
    } else {
      if (usedType === 'ali') {
        ret = await getPosterUrl(getPosterTemplate1_1(posterInfo, '1', good, usedType), usedType, '', '', true, '', '1')
      } else {
        ret = await getPosterUrl(getPosterTemplateLianZero(posterInfo, good, localMealId), usedType)
      }
    }
    if (ret) {
      return {url: ret}
    } else {
      getPosterError()
      return false
    }
  } else {
    getPosterInfoError()
    return false
  }
}

const getMoviePoster = async (data: any, img?: string) => {
  let posterInfoRet: Poster | false = false
  posterInfoRet = await http.post<Poster>(apiUrl['movie'], {
    path: data.path + '?scene=' + data.scene + `&id=${data.cinemaId || ''}&mid=${data.movieId}`,
    pageType: data.pageType,
    tempSellerId: data.tempSellerId
  })
  if (posterInfoRet) {
    const posterInfo = posterInfoRet
    if (img) {
      posterInfo.goodsBackground = img
    }
    let ret: false | string = false
    if (posterInfo.imgCustomize) {
      ret = await getPosterUrl(posterInfo.imgCustomize, 'movie', posterInfo.imgCustomizeKey);
    } else {
      ret = await getPosterUrl(getPosterTemplate0(posterInfo), 'movie')
    }
    if (ret) {
      return {url: ret}
    } else {
      getPosterError()
      return false
    }
  } else {
    getPosterInfoError()
    return false
  }
}

const getSharePoster = async (data: any): Promise<{ url: string } | false> => {
  if (!process.env.SHOW_SHARE_POSTER) {
    return {url: ''}
  }
  const params = getSharePosterTemplate(data);
  const name = data.uuid || ''
  const bk = 'resources/system/img/mshare.png'
  const ret = await getPosterUrl(params, 'share' + name, '', bk, true, data.isDetail, data.posterType || '1')
  if (ret) {
    return {url: ret}
  } else {
    getPosterError()
    return false
  }
}

const getLivePosterTemplate = (data: Tempalte2): string => {
  return (
    `[
            {
              "type": "image",
              "url": "${data.posterImgUrl}",
              "coord": "720x551!+0+0"
            },
            {
              "type": "image",
              "url": "${data.QRcode}",
              "coord": "180x180!+25+571"
            },
            {
              "type": "text",
              "coord": "+45+751",
              "text": "长按识别二维码",
              "font-size": 22
            },
            {
              "type": "text",
              "coord": "+260+591",
              "font-size": 32,
              "text": "${(data.title).substring(0, 14)}"
            },
            {
                "type": "text",
                "coord": "+260+628",
                "font-size": 32,
                "text": "${(data.title).substring(14)}"
            },
            {
                "type": "text",
                "coord": "+260+720",
                "font-size": 32,
                "text": "${data.shareName}"
            }
          ]`
  )
}

const getLivePoster = async (data: Tempalte2) => {
  const params = getLivePosterTemplate(data);
  const name = new Date().getTime();
  const bk = 'resources/system/img/bk_801.png'
  const ret = await getPosterUrl(params, 'text' + name, bk)
  if (ret) {
    return {url: ret}
  } else {
    getPosterError()
    return false
  }
}

export {
  getPoster,
  getMoviePoster,
  getSharePoster,
  getLivePoster,
  getLianPosterInfo,
  getPosterUrl,
  getPosterUrlForSeckillList,
  getPosterTemplate0,
  getPosterTemplateLianZero
}

